home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / REGUTIL.PY < prev    next >
Encoding:
Text File  |  1998-07-15  |  11.0 KB  |  282 lines

  1. # Some registry helpers.
  2. import win32api
  3. import win32con
  4. import string
  5. import sys
  6. import os
  7.  
  8. error = "Registry utility error"
  9.  
  10. # A .py file has a CLSID associated with it (why? - dunno!)
  11. CLSIDPyFile = "{b51df050-06ae-11cf-ad3b-524153480001}"
  12.  
  13. RegistryIDPyFile = "Python.File" # The registry "file type" of a .py file
  14. RegistryIDPycFile = "Python.CompiledFile" # The registry "file type" of a .pyc file
  15.  
  16. def GetRootKey():
  17.     """Retrieves the Registry root in use by Python.
  18.     """
  19. # Win32s no longer supported/released.
  20. #    if win32ui.IsWin32s():
  21. #        return win32con.HKEY_CLASSES_ROOT
  22. #    else:
  23.     return win32con.HKEY_LOCAL_MACHINE
  24.  
  25. def GetRegistryDefaultValue(subkey, rootkey = None):
  26.     """A helper to return the default value for a key in the registry.
  27.         """
  28.     if rootkey is None: rootkey = GetRootKey()
  29.     return win32api.RegQueryValue(rootkey, subkey)
  30.  
  31. def SetRegistryDefaultValue(subKey, value, rootkey = None):
  32.     """A helper to set the default value for a key in the registry
  33.         """
  34.     import types
  35.     if rootkey is None: rootkey = GetRootKey()
  36.     if type(value)==types.StringType:
  37.         typeId = win32con.REG_SZ
  38.     elif type(value)==types.IntType:
  39.         typeId = win32con.REG_DWORD
  40.     else:
  41.         raise TypeError, "Value must be string or integer - was passed " + str(value)
  42.  
  43.     win32api.RegSetValue(rootkey, subKey, typeId ,value)
  44.     
  45. def BuildDefaultPythonKey():
  46.     """Builds a string containing the path to the current registry key.
  47.  
  48.        The Python registry key contains the Python version.  This function
  49.        uses the version of the DLL used by the current process to get the
  50.        registry key currently in use.
  51.         """
  52.  
  53.     return "Software\\Python\\PythonCore\\" + sys.winver
  54.  
  55. def GetAppPathsKey():
  56.     return "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths"
  57.  
  58. def RegisterPythonExe(exeFullPath, exeAlias = None, exeAppPath = None):
  59.     """Register a .exe file that uses Python.
  60.  
  61.        Registers the .exe with the OS.  This allows the specified .exe to
  62.        be run from the command-line or start button without using the full path,
  63.        and also to setup application specific path (ie, os.environ['PATH']).
  64.  
  65.        Currently the exeAppPath is not supported, so this function is general
  66.        purpose, and not specific to Python at all.  Later, exeAppPath may provide
  67.        a reasonable default that is used.
  68.  
  69.        exeFullPath -- The full path to the .exe
  70.        exeAlias = None -- An alias for the exe - if none, the base portion
  71.                  of the filename is used.
  72.        exeAppPath -- Not supported.
  73.     """
  74.     # Note - Dont work on win32s (but we dont care anymore!)
  75.     if exeAppPath:
  76.         raise error, "Do not support exeAppPath argument currently"
  77.     if exeAlias is None:
  78.         exeAlias = os.path.basename(exeFullPath)
  79.     win32api.RegSetValue(GetRootKey(), GetAppPathsKey() + "\\" + exeAlias, win32con.REG_SZ, exeFullPath)
  80.  
  81. def GetRegisteredExe(exeAlias):
  82.     """Get a registered .exe
  83.     """
  84.     return win32api.RegQueryValue(GetRootKey(), GetAppPathsKey() + "\\" + exeAlias)
  85.  
  86. def UnregisterPythonExe(exeAlias):
  87.     """Unregister a .exe file that uses Python.
  88.     """
  89.     try:
  90.         win32api.RegDeleteKey(GetRootKey(), GetAppPathsKey() + "\\" + exeAlias)
  91.     except win32api.error, (code, fn, details):
  92.         import winerror
  93.         if code!=winerror.ERROR_FILE_NOT_FOUND:
  94.             raise win32api.error, (code, fn, desc)
  95.         return
  96.  
  97. def RegisterNamedPath(name, path):
  98.     """Register a named path - ie, a named PythonPath entry.
  99.     """
  100.     keyStr = BuildDefaultPythonKey() + "\\PythonPath"
  101.     if name: keyStr = keyStr + "\\" + name
  102.     win32api.RegSetValue(GetRootKey(), keyStr, win32con.REG_SZ, path)
  103.  
  104. def UnregisterNamedPath(name):
  105.     """Unregister a named path - ie, a named PythonPath entry.
  106.     """
  107.     keyStr = BuildDefaultPythonKey() + "\\PythonPath\\" + name
  108.     try:
  109.         win32api.RegDeleteKey(GetRootKey(), keyStr)
  110.     except win32api.error, (code, fn, details):
  111.         import winerror
  112.         if code!=winerror.ERROR_FILE_NOT_FOUND:
  113.             raise win32api.error, (code, fn, desc)
  114.         return
  115.  
  116. def GetRegisteredNamedPath(name):
  117.     """Get a registered named path, or None if it doesnt exist.
  118.     """
  119.     keyStr = BuildDefaultPythonKey() + "\\PythonPath"
  120.     if name: keyStr = keyStr + "\\" + name
  121.     try:
  122.         return win32api.RegQueryValue(GetRootKey(), keyStr)
  123.     except win32api.error, (code, fn, details):
  124.         import winerror
  125.         if code!=winerror.ERROR_FILE_NOT_FOUND:
  126.             raise win32api.error, (code, fn, desc)
  127.         return
  128.  
  129.  
  130. def RegisterModule(modName, modPath):
  131.     """Register an explicit module in the registry.  This forces the Python import
  132.            mechanism to locate this module directly, without a sys.path search.  Thus
  133.            a registered module need not appear in sys.path at all.
  134.  
  135.        modName -- The name of the module, as used by import.
  136.        modPath -- The full path and file name of the module.
  137.     """
  138.     try:
  139.         import os
  140.         os.stat(modPath)
  141.     except os.error:
  142.         print "Warning: Registering non-existant module %s" % modPath
  143.     win32api.RegSetValue(GetRootKey(), 
  144.                          BuildDefaultPythonKey() + "\\Modules\\%s" % modName,
  145.         win32con.REG_SZ, modPath)
  146.  
  147. def UnregisterModule(modName):
  148.     """Unregister an explicit module in the registry.
  149.  
  150.        modName -- The name of the module, as used by import.
  151.     """
  152.     try:
  153.         win32api.RegDeleteKey(GetRootKey(), 
  154.                              BuildDefaultPythonKey() + "\\Modules\\%s" % modName)
  155.     except win32api.error, (code, fn, desc):
  156.         import winerror
  157.         if code!=winerror.ERROR_FILE_NOT_FOUND:
  158.             raise win32api.error, (code, fn, desc)
  159.  
  160. def GetRegisteredHelpFile(helpDesc):
  161.     """Given a description, return the registered entry.
  162.     """
  163.     try:
  164.         return GetRegistryDefaultValue(BuildDefaultPythonKey() + "\\Help\\" + helpDesc)
  165.     except win32api.error:
  166.         return None
  167.  
  168. def RegisterHelpFile(helpFile, helpPath, helpDesc = None, bCheckFile = 1):
  169.     """Register a help file in the registry.
  170.     
  171.          Note that this used to support writing to the Windows Help
  172.          key, however this is no longer done, as it seems to be incompatible.
  173.  
  174.            helpFile -- the base name of the help file.
  175.            helpPath -- the path to the help file
  176.            helpDesc -- A description for the help file.  If None, the helpFile param is used.
  177.            bCheckFile -- A flag indicating if the file existence should be checked.
  178.     """
  179.     if helpDesc is None: helpDesc = helpFile
  180.     fullHelpFile = os.path.join(helpPath, helpFile)
  181.     try:
  182.         if bCheckFile: os.stat(fullHelpFile)
  183.     except os.error:
  184.         raise ValueError, "Help file does not exist"
  185.     # Now register with Python itself.
  186.     win32api.RegSetValue(GetRootKey(), 
  187.                          BuildDefaultPythonKey() + "\\Help\\%s" % helpDesc, win32con.REG_SZ, fullHelpFile)
  188.  
  189. def UnregisterHelpFile(helpFile, helpDesc = None):
  190.     """Unregister a help file in the registry.
  191.  
  192.            helpFile -- the base name of the help file.
  193.            helpDesc -- A description for the help file.  If None, the helpFile param is used.
  194.     """
  195.     key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\Help", 0, win32con.KEY_ALL_ACCESS)
  196.     try:
  197.         try:
  198.             win32api.RegDeleteValue(key, helpFile)
  199.         except win32api.error, (code, fn, desc):
  200.             import winerror
  201.             if code!=winerror.ERROR_FILE_NOT_FOUND:
  202.                 raise win32api.error, (code, fn, desc)
  203.     finally:
  204.         win32api.RegCloseKey(key)
  205.     
  206.     # Now de-register with Python itself.
  207.     if helpDesc is None: helpDesc = helpFile
  208.     try:
  209.         win32api.RegDeleteKey(GetRootKey(), 
  210.                              BuildDefaultPythonKey() + "\\Help\\%s" % helpDesc)    
  211.     except win32api.error, (code, fn, desc):
  212.         import winerror
  213.         if code!=winerror.ERROR_FILE_NOT_FOUND:
  214.             raise win32api.error, (code, fn, desc)
  215.  
  216. def RegisterCoreDLL(coredllName = None):
  217.     """Registers the core DLL in the registry.
  218.  
  219.         If no params are passed, the name of the Python DLL used in 
  220.         the current process is used and registered.
  221.     """
  222.     if coredllName is None:
  223.         coredllName = win32api.GetModuleFileName(sys.dllhandle)
  224.         # must exist!
  225.     else:
  226.         try:
  227.             os.stat(coredllName)
  228.         except os.error:
  229.             print "Warning: Registering non-existant core DLL %s" % coredllName
  230.  
  231.     hKey = win32api.RegCreateKey(GetRootKey() , BuildDefaultPythonKey())
  232.     try:
  233.         win32api.RegSetValue(hKey, "Dll", win32con.REG_SZ, coredllName)
  234.     finally:
  235.         win32api.RegCloseKey(hKey)
  236.     # Lastly, setup the current version to point to me.
  237.     win32api.RegSetValue(GetRootKey(), "Software\\Python\\PythonCore\\CurrentVersion", win32con.REG_SZ, sys.winver)
  238.  
  239. def RegisterFileExtensions(defPyIcon, defPycIcon, runCommand):
  240.     """Register the core Python file extensions.
  241.     
  242.        defPyIcon -- The default icon to use for .py files, in 'fname,offset' format.
  243.        defPycIcon -- The default icon to use for .pyc files, in 'fname,offset' format.
  244.        runCommand -- The command line to use for running .py files
  245.     """
  246.     # Register the file extensions.
  247.     pythonFileId = RegistryIDPyFile
  248.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , ".py", win32con.REG_SZ, pythonFileId)
  249.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , pythonFileId , win32con.REG_SZ, "Python File")
  250.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , "%s\\CLSID" % pythonFileId , win32con.REG_SZ, CLSIDPyFile)
  251.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , "%s\\DefaultIcon" % pythonFileId, win32con.REG_SZ, defPyIcon)
  252.     base = "%s\\Shell" % RegistryIDPyFile
  253.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\Open", win32con.REG_SZ, "Run")
  254.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\Open\\Command", win32con.REG_SZ, runCommand)
  255.  
  256.     # Register the .PYC.
  257.     pythonFileId = RegistryIDPycFile
  258.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , ".pyc", win32con.REG_SZ, pythonFileId)
  259.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , pythonFileId , win32con.REG_SZ, "Compiled Python File")
  260.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , "%s\\DefaultIcon" % pythonFileId, win32con.REG_SZ, defPycIcon)
  261.     base = "%s\\Shell" % pythonFileId
  262.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\Open", win32con.REG_SZ, "Run")
  263.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\Open\\Command", win32con.REG_SZ, runCommand)
  264.  
  265. def RegisterShellCommand(shellCommand, exeCommand, shellUserCommand = None):
  266.     # Last param for "Open" - for a .py file to be executed by the command line
  267.     # or shell execute (eg, just entering "foo.py"), the Command must be "Open",
  268.     # but you may associate a different name for the right-click menu.
  269.     # In our case, normally we have "Open=Run"
  270.     base = "%s\\Shell" % RegistryIDPyFile
  271.     if shellUserCommand:
  272.         win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s" % (shellCommand), win32con.REG_SZ, shellUserCommand)
  273.  
  274.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s\\Command" % (shellCommand), win32con.REG_SZ, exeCommand)
  275.  
  276. def RegisterDDECommand(shellCommand, ddeApp, ddeTopic, ddeCommand):
  277.     base = "%s\\Shell" % RegistryIDPyFile
  278.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s\\ddeexec" % (shellCommand), win32con.REG_SZ, ddeCommand)
  279.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s\\ddeexec\\Application" % (shellCommand), win32con.REG_SZ, ddeApp)
  280.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s\\ddeexec\\Topic" % (shellCommand), win32con.REG_SZ, ddeTopic)
  281.  
  282.